//============================================================================
// Name        : Ensemble_Classifiers.h
// Author      : Andrey Bicalho Santos e Clayson Sandro Celes
// Version     :
// Copyright   : Your copyright notice
// Description : Genetic Algorithm in C++, Ansi-style
//============================================================================

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <cstring>
#include <vector>

using namespace std;

int converteBooleano(int *cromo, int inicio, int fim);
void printData(float **data, int x, int y);
int count_lines(const char* fileName);
void load_file(const char* fileName);
int converteBooleano(int *cromo, int inicio, int fim);
int **bin2dec_pop(Individual *bin_population, int num_individuals);
void printBidimensionalMatriz(int **matriz, int x, int y);

void split(const std::string & str, const string & delimiters, std::vector<std::string> * elems) {

	// Skip delimiters at beginning.
	string::size_type lastPos = str.find_first_not_of(delimiters, 0);
	// Find first "non-delimiter".
	string::size_type pos = str.find_first_of(delimiters, lastPos);

	while (string::npos != pos || string::npos != lastPos) {
		// Found a token, add it to the vector.
		elems->push_back(str.substr(lastPos, pos - lastPos));
		// Skip delimiters.  Note the "not_of"
		lastPos = str.find_first_not_of(delimiters, pos);
		// Find next "non-delimiter"
		pos = str.find_first_of(delimiters, lastPos);
	}

}

void printData(float **data, int x, int y) {
	printf("\n");
	for (int i = 0; i < x; i++) {
		for (int j = 0; j < y; j++)
			cout << " " <<  data[i][j];
		printf("\n");
	}
}

int count_lines(const char* fileName) {
	int linhas = 1;
	ifstream infile;
	infile.open(fileName); // open file
	string line;
	while (getline(infile, line)) {
		linhas++;
	}

	return linhas;
}

//------------------------------------------------------------------------------
void load_file_config_1(const char* fileName) {
	int i_linha = count_lines(fileName);
	//float data[i_linha][4]; // trocar por ponteiro
	float **data;
	data = new float*[i_linha];
	for(int i=0;i < i_linha;i++)
			data[i] = new float[4]; // In this problem, there are 4 important values (real and predicted classes)

	ifstream infile;
	infile.open(fileName); // open file
	if (infile) {
		string line;
		int j = 0;
		while (getline(infile, line)) {
			string temp;
			vector<string> myvector;
			split(line, " ", &myvector);
			for (int i = 0; i < myvector.size(); i++) {
				temp = myvector.at(i);
				if (i == 1) {
					data[j][0] = atof(temp.substr(6, 6).c_str());
				} else if (i > 1) {
					int y = atoi(temp.substr(6, 6).c_str());
					data[j][y] = atof(temp.substr(8, temp.length()).c_str());
				}
			}
			j++;
		}
	}

	printData(data,i_linha, 4);
	//teste - somente para imprimir o vetor
/*	for (int i = 0; i < i_linha; i++) {
		for (int j = 0; j < 4; j++)
			cout << data[i][j] << " ";
		printf("\n");
	}*/
}
//------------------------------------------------------------------------------
int converteBooleano(int *cromo, int inicio, int fim){
	int i, inc = 1;
	int aux=0;
	for(i=fim; i>=inicio; i--){
		if(cromo[i] == 1)
			aux += inc;

		inc *= 2;
	}
	return aux;
}
//------------------------------------------------------------------------------
int **bin2dec_pop(Individual *bin_population, int num_individuals){
	int **dec_population;
	dec_population = new int*[num_individuals];
	for(int i=0;i < num_individuals;i++){
		dec_population[i] = new int[3]; //3 classificadores
	}

	int weight_size = 0;
	for(int i = 0; i < num_individuals; i++){
		weight_size = bin_population[i].getNumGenes()/3; //3 classificadores
		for(int j=0; j < 3; j++){
			dec_population[i][j] = converteBooleano(bin_population[i].getChromosome(),weight_size*j, weight_size*(j+1)-1);
		}
	}
	return dec_population;
}
//------------------------------------------------------------------------------
void printBidimensionalMatriz(int **matriz, int x, int y){
	printf("\n");
	for(int i=0;i<x;i++){
		for(int j=0;j<y;j++)
			printf("%d ",matriz[i][j]);
		printf("\n");
	}
}
